#include "mod.hpp"
#include "db_manager.hpp"
#include <sstream>

std::vector<Mod> Mod::getAll() {
    std::vector<Mod> mods;
    auto* mysql = DBManager::getInstance().getConnection();
    
    const char* query = "SELECT id, modid, content FROM ModIDConfig";
    if (mysql_query(mysql, query)) {
        return mods;
    }
    
    MYSQL_RES* result = mysql_store_result(mysql);
    if (!result) {
        return mods;
    }
    
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))) {
        Mod mod;
        mod.id = std::stoi(row[0]);
        mod.modid = std::stoi(row[1]);
        mod.content = row[2] ? row[2] : "";
        mods.push_back(mod);
    }
    
    mysql_free_result(result);
    return mods;
}

Mod Mod::getById(int id) {
    Mod mod;
    auto* mysql = DBManager::getInstance().getConnection();
    
    std::stringstream ss;
    ss << "SELECT id, modid, content FROM ModIDConfig WHERE id = " << id;
    
    if (mysql_query(mysql, ss.str().c_str())) {
        return mod;
    }
    
    MYSQL_RES* result = mysql_store_result(mysql);
    if (!result) {
        return mod;
    }
    
    MYSQL_ROW row = mysql_fetch_row(result);
    if (row) {
        mod.id = std::stoi(row[0]);
        mod.modid = std::stoi(row[1]);
        mod.content = row[2] ? row[2] : "";
    }
    
    mysql_free_result(result);
    return mod;
}

bool Mod::add(int modid, const std::string& content) {
    auto* mysql = DBManager::getInstance().getConnection();
    
    char escaped_content[512];
    mysql_real_escape_string(mysql, escaped_content, content.c_str(), content.length());
    
    std::stringstream ss;
    ss << "INSERT INTO ModIDConfig (modid, content) VALUES (" << modid << ", '" << escaped_content << "')";
    
    return mysql_query(mysql, ss.str().c_str()) == 0;
}

bool Mod::update(int id, const std::string& content) {
    auto* mysql = DBManager::getInstance().getConnection();
    
    char escaped_content[512];
    mysql_real_escape_string(mysql, escaped_content, content.c_str(), content.length());
    
    std::stringstream ss;
    ss << "UPDATE ModIDConfig SET content = '" << escaped_content << "' WHERE id = " << id;
    
    return mysql_query(mysql, ss.str().c_str()) == 0;
}

bool Mod::remove(int id) {
    auto* mysql = DBManager::getInstance().getConnection();
    
    std::stringstream ss;
    ss << "DELETE FROM ModIDConfig WHERE id = " << id;
    
    return mysql_query(mysql, ss.str().c_str()) == 0;
}

bool Mod::exists(int modid) {
    auto* mysql = DBManager::getInstance().getConnection();
    std::stringstream ss;
    ss << "SELECT COUNT(*) FROM ModIDConfig WHERE modid = " << modid;
    if (mysql_query(mysql, ss.str().c_str())) {
        return false;
    }
    MYSQL_RES* result = mysql_store_result(mysql);
    if (!result) {
        return false;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    bool found = false;
    if (row && row[0]) {
        found = (std::stoi(row[0]) > 0);
    }
    mysql_free_result(result);
    return found;
} 